#!/usr/bin/env python
#-*- coding: utf_8 -*-
#
# Copyright 2010 Stanley Cai
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# 
#   http://www.apache.org/licenses/LICENSE-2.0
# 
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

__author__= "Stanley Cai"
__email__ = "stanley.w.cai@gmail.com"

import urllib2
import urllib
import re

def get_content_by_page(url, post_data, pages):
  count = 3
  post_data['pID'] = str(pages)
  params = urllib.urlencode(post_data)
  while 1:
    try:
      f = urllib2.urlopen(url, params)
      data = f.read()
      # stupid tianya uses a mixed charset string
      data = data.decode("gbk", "ignore").encode("utf_8")
      f.close()
      break
    except:
      count -= 1
      if count == 0:
        return None
  return data

def get_pages(cont, url):
  pd = get_post_data(cont)
  str = '<font color="black">共'
  p0 = cont.find(str, 0)
  if p0 < 0:
    return
  p1 = cont.find('页', p0)
  if p1 < 0:
    return
  
  yield cont
  pages = int(cont[p0+len(str):p1])
  for i in range(2, pages+1):
    post = get_content_by_page(url, pd, i)
    yield post    

def get_post_data(cont):
  pd = {}
  p0 = 0
  p1 = cont.find('<table align=left><tr><form name="pageForm" id="pageForm"'
                 ' action="" method="post"><td align=left>', p0)
  if p1 < 0:
    return None
  p2 = cont.find('<font color="black">共',p1)
  if p2 < 0:
    return None
  s1 = cont[p1:p2]
  reg = re.compile(r'<input type="hidden" name="(\w+)" value="([^"]+)">')
  for mo in reg.findall(s1):
    pd[mo[0]] = mo[1]
  return pd

def get_author(cont):
  p1 = cont.find("<TABLE")
  if p1 < 0:
    return None
  p1 = cont.find('>作者：<a', p1)
  if p1 < 0:
    return None
  p1 = cont.find("vwriter=", p1)
  if p1 < 0:
    return None
  s1 = "target=_blank>"
  p1 = cont.find(s1, p1)
  if p1 < 0:
    return None
  s2 = "</a>"
  p2 = cont.find(s2, p1)
  if p2 < 0:
    return None
  return cont[p1+len(s1):p2]

def get_title(cont):
  s1 = '<title>'
  p1 = cont.find(s1)
  if p1 < 0:
    return None
  p2 = cont.find('</title>', p1)
  if p2 < 0:
    return None
  return cont[p1 + len(s1):p2]

def get_by_author(cont,author):
  p0 = 0
  res = []
  while 1:
    p1 = cont.find("<TABLE",p0)
    if p1 < 0:
      break
    p2 = cont.find(">作者：<a", p1)
    if p2 < 0:
      break    
    p2 = cont.find(">"+author+"</a>", p2)
    if p2 < 0:
      break
    time = cont.find("日期：", p2)
    if time < 0:
      break
    font = cont.find("</font>", time)
    if font < 0:
      break
    p3 = cont.find("</table>", p2)
    if p3 < 0:
      break
    p4 = cont.find("</DIV", p3)
    if p4 < 0:
      p4 = cont.find("<!-- google_ad_section_end -->", p3)
      res.append(cont[p3+8:p4+5])
      break
    else:
      p0 = p4
      res.append("<B>" + author)
      res.append(cont[time:font])
      res.append("</B>")
      res.append(cont[p3+8:p4+5])
  return res
